/** 
 * @Title: Data2File.java 
 * @Package participle.file 
 * @Description: TODO
 * @author HeYan
 * @date 2016年9月26日 下午6:49:03 
 */
package com.zizo.test;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.zizo.util.ConnectionPool;

/**
 * @ClassName: Data2File
 * @Description: TODO
 * @author HeYan
 * @date 2016年9月26日 下午6:49:03
 * 
 */
public class Data2FileRel implements Runnable {
	private Connection conn;
	private BufferedWriter br;
	private String table;

	/**
	 * @param conn
	 * @param br
	 * @param table
	 */
	public Data2FileRel(Connection conn, String table) {
		super();
		this.conn = conn;
		try {
			File file = new File("C:\\Users\\Apple\\Desktop\\" + "a.txt");
			if (!file.exists()) {
				file.createNewFile();
			}
			this.br = new BufferedWriter(new FileWriter(file, true));
		} catch (IOException e) {
			e.printStackTrace();
		}
		this.table = table;
	}

	public Data2FileRel(Connection conn) {
		super();
		this.conn = conn;
	}

	public List<String> getTables() {
		List<String> list = new ArrayList<>();
		Statement st = null;
		ResultSet rs = null;
		String sql = "SELECT `table` as a FROM `table_word`";
		try {
			st = conn.createStatement();
			rs = st.executeQuery(sql);
			while (rs.next()) {
				list.add(rs.getString(1));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	public void getDataFromBBS() throws IOException {
		Statement st = null;
		ResultSet rs = null;
		String sql = "SELECT zj_title,zj_content FROM " + table;
		try {
			st = conn.createStatement();
			rs = st.executeQuery(sql);
			while (rs.next()) {
				String s1 = rs.getString(1);
				String s2 = rs.getString(2);
				if (s1 != null)
					insertData2File(s1);
				if (s2 != null)
					insertData2File(s2);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public final String getFilterData(String content) {
		String s = "[；。\\+\\——]|�|÷|→|※|∧|∥|∏|∑|√|∩|∪|∫|∮|☆|★|⊥|∷|∵|∨|⊙|□|▪|◎|〓|↑|〖|〗|＆|Ā|Ō|°|♀|☊ |々|「|」|￼|￣|\\*|・|ˇ|°|￥|￡|≈|≌|√|∠|∈|\\||∞|Š|Ū|Ǔ|Ò|Ê|=|\\d{6,}|、|\\?{3,}|▲|～|…{2,}|·|●|[A-Za-z]{6,}|―{3,}|◆|■|℃|%|％|\\.|￣|∀|｀|＾|］|⌒|＠{2,}|＄|＃|§| {3,}|﹏|　{2,}";
		Pattern p_1 = Pattern.compile(s);
		Matcher m_1 = p_1.matcher(content);
		String res_1 = m_1.replaceAll("");
		return res_1;
	}

	public void insertData2File(String data) throws IOException {
		br.write(getFilterData(data.replaceAll(
				"(http:?//|ftp:?//|https:?//|www){0,1}[^\u4e00-\u9fa5\\s]*?\\.(com|net|cn|me|tw|fr)[^\u4e00-\u9fa5\\s]*",
				"")) + "\n");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Runnable
	 */
	@Override
	public void run() {
		try {
			getDataFromBBS();
			System.out.println(table);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void main(String[] args) throws IOException {
		ExecutorService fix = Executors.newFixedThreadPool(50);
		Connection conn = ConnectionPool.getConnection("ProjectTemp", "server.so.car", "3308");
		fix.execute(new Data2FileRel(conn, "zj"));
		fix.shutdown();
	}
}
